package com.baijiayun.liveuibase.widgets.setting;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.view.*;
import android.widget.SeekBar;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatSeekBar;
import com.baijiayun.livecore.context.LPConstants;
import com.baijiayun.livecore.models.LPMirrorModeModel;
import com.baijiayun.livecore.utils.LPRxUtils;
import com.baijiayun.liveuibase.R;
import com.baijiayun.liveuibase.base.BaseDialogFragment;
import com.baijiayun.liveuibase.base.QueryPlus;
import com.baijiayun.liveuibase.utils.DisplayUtils;
import com.baijiayun.liveuibase.utils.ThemeDataUtil;
import com.baijiayun.liveuibase.utils.drawable.StateListDrawableBuilder;
import com.baijiayun.liveuibase.widgets.SettingSwitch;
import com.baijiayun.liveuibase.widgets.SwitchCompat.MultiSwitchCompat;
import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author wubanglin
 */
public abstract class BaseSettingFragment extends BaseDialogFragment implements BaseSettingContract.BaseSettingView {
    protected Context context;
    private ClipboardManager clipboardManager;
    protected BaseSettingContract.BaseSettingPresenter presenter;
    private ViewGroup settingContainer;
    private TextView lastSelectedView;
    private LPMirrorModeModel lastMirrorModeModel = new LPMirrorModeModel();
    private final Map<String, AtomicBoolean> clickableWithKey = new ConcurrentHashMap<>();

    private Disposable disposableOfTimer;

    private boolean isBeautySwitchCheck;
    /** 初始默认值50% **/
    private float lastBeautyLevel = 0.5f;
    private float lastWhitenessLevel = 0.5f;

    @Override
    protected int getLayoutId() {
        return R.layout.uibase_setting_fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        context = getContext();
        clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        presenter = initPresenter();
        initChildData();
        return view;
    }

    @Override
    public void onViewStateRestored(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
        // initView()需要放在onViewStateRestored()之后，否则 MultiSwitchCompact.isChecked()默认为false会覆盖setCheckedPosition()设置的值
        initView();
    }

    public void setPresenter(BaseSettingContract.BaseSettingPresenter presenter) {
        super.setBasePresenter(presenter);
        this.presenter = presenter;
    }

    @Override
    protected void init(Bundle savedInstanceState, Bundle arguments) {
        super.title(getString(R.string.bjy_base_setting_title));
        super.showCross();
    }

    @Override
    protected void setWindowParams(WindowManager.LayoutParams windowParams) {
        super.setWindowParams(windowParams);
        windowParams.width = DisplayUtils.dip2px(context, 520);
        windowParams.height = DisplayUtils.dip2px(context, 300);
        windowParams.gravity = Gravity.CENTER;
        windowParams.windowAnimations = R.style.BJYBaseUITransYDialogAnim;
    }

    private void initView() {
        settingContainer = $.id(R.id.bjy_base_setting_container).view();
        TextView roomIdTv = $.id(R.id.bjy_base_setting_room_id_textview).view();
        $ = QueryPlus.with(contentView);
        roomIdTv.setText(getResources().getString(R.string.bjy_base_setting_class_id, presenter.getRoomId()));
        TextView copyRoomIdTv = $.id(R.id.bjy_base_setting_copy_room_id).view();
        copyRoomIdTv.setOnClickListener(v -> {
            ClipData clipData = ClipData.newPlainText("roomId", roomIdTv.getText().toString());
            clipboardManager.setPrimaryClip(clipData);
            showToast(getResources().getString(R.string.bjy_base_setting_copy_class_id));
        });
        showCameraContainer();
        lastSelectedView = $.id(R.id.bjy_base_setting_type_camera).view();
        if (!presenter.getPartnerConfig().liveEnableUseHandWritingBoard) {
            $.id(R.id.bjy_base_setting_type_other).visibility(View.GONE);
        }
        $.id(R.id.bjy_base_setting_type_camera).clicked(v -> {
            showCameraContainer();
            lastSelectedView = $.id(R.id.bjy_base_setting_type_camera).view();
        });
        $.id(R.id.bjy_base_setting_type_mic).clicked(v -> {
            showMicContainer();
            lastSelectedView = $.id(R.id.bjy_base_setting_type_mic).view();
        });
        $.id(R.id.bjy_base_setting_type_room_control).clicked(v -> {
            showRoomControlContainer();
            lastSelectedView = $.id(R.id.bjy_base_setting_type_room_control).view();
        });
        $.id(R.id.bjy_base_setting_type_ppt).clicked(v -> {
            showPPTContainer();
            lastSelectedView = $.id(R.id.bjy_base_setting_type_ppt).view();
        });
        $.id(R.id.bjy_base_setting_type_beauty).clicked(v -> {
            showBeautyContainer();
            lastSelectedView = $.id(R.id.bjy_base_setting_type_beauty).view();
        });
        $.id(R.id.bjy_base_setting_type_other).clicked(v -> {
            showOtherContainer();
            lastSelectedView = $.id(R.id.bjy_base_setting_type_other).view();
        });
        updateBeautyVisible();
        LPConstants.LPRoomType roomType = presenter.getLiveRoom().getRoomInfo().roomType;
        if (roomType == LPConstants.LPRoomType.NewSmallGroup) {
            $.id(R.id.bjy_base_setting_type_mic).visibility(View.GONE);
            if (!presenter.getLiveRoom().isTeacherOrAssistant() &&
                    !presenter.getLiveRoom().getPartnerConfig().enableHideOtherStudentVideo) {
                $.id(R.id.bjy_base_setting_type_room_control).visibility(View.GONE);
            }
        }
    }

    private void showCameraContainer() {
        ((TextView) $.id(R.id.bjy_base_setting_type_camera).view()).setTextColor(Color.WHITE);
        if (lastSelectedView != null) {
            lastSelectedView.setTextColor(ThemeDataUtil.getColorFromThemeConfigByAttrId(context, R.attr.base_theme_window_main_text_color));
            lastSelectedView.setSelected(false);
        }
        $.id(R.id.bjy_base_setting_type_camera).view().setSelected(true);
        settingContainer.removeAllViews();
        LayoutInflater.from(context).inflate(R.layout.bjy_base_layout_setting_camera, settingContainer);
        initCameraContainer();
    }

    protected abstract void initCameraContainer();

    private void showMicContainer() {
        ((TextView) $.id(R.id.bjy_base_setting_type_mic).view()).setTextColor(Color.WHITE);
        lastSelectedView.setTextColor(ThemeDataUtil.getColorFromThemeConfigByAttrId(context, R.attr.base_theme_window_main_text_color));
        lastSelectedView.setSelected(false);
        $.id(R.id.bjy_base_setting_type_mic).view().setSelected(true);
        settingContainer.removeAllViews();
        LayoutInflater.from(context).inflate(R.layout.bjy_base_layout_setting_mic, settingContainer);
        initMicContainer();
    }

    protected abstract void initMicContainer();

    private void showRoomControlContainer() {
        ((TextView) $.id(R.id.bjy_base_setting_type_room_control).view()).setTextColor(Color.WHITE);
        lastSelectedView.setTextColor(ThemeDataUtil.getColorFromThemeConfigByAttrId(context, R.attr.base_theme_window_main_text_color));
        lastSelectedView.setSelected(false);
        $.id(R.id.bjy_base_setting_type_room_control).view().setSelected(true);
        settingContainer.removeAllViews();
        LayoutInflater.from(context).inflate(R.layout.bjy_base_layout_setting_room_control, settingContainer);
        initRoomControlContainer();
    }

    protected abstract void initRoomControlContainer();

    public void hideMirrorMenu() {
        $.id(R.id.bjy_base_setting_all_horizontal_mirror_text).visibility(View.GONE);
        $.id(R.id.bjy_base_setting_all_horizontal_mirror_open).visibility(View.GONE);
        $.id(R.id.bjy_base_setting_all_horizontal_mirror_close).visibility(View.GONE);
        $.id(R.id.bjy_base_setting_all_vertical_mirror_text).visibility(View.GONE);
        $.id(R.id.bjy_base_setting_all_vertical_mirror_open).visibility(View.GONE);
        $.id(R.id.bjy_base_setting_all_vertical_mirror_close).visibility(View.GONE);
    }

    private void showPPTContainer() {
        ((TextView) $.id(R.id.bjy_base_setting_type_ppt).view()).setTextColor(Color.WHITE);
        lastSelectedView.setTextColor(ThemeDataUtil.getColorFromThemeConfigByAttrId(context, R.attr.base_theme_window_main_text_color));
        lastSelectedView.setSelected(false);
        $.id(R.id.bjy_base_setting_type_ppt).view().setSelected(true);
        settingContainer.removeAllViews();
        LayoutInflater.from(context).inflate(R.layout.bjy_base_layout_setting_ppt, settingContainer);
        initPPTContainer();
    }

    protected abstract void initPPTContainer();

    private void showBeautyContainer() {
        ((TextView) $.id(R.id.bjy_base_setting_type_beauty).view()).setTextColor(Color.WHITE);
        lastSelectedView.setTextColor(ThemeDataUtil.getColorFromThemeConfigByAttrId(context, R.attr.base_theme_window_main_text_color));
        lastSelectedView.setSelected(false);
        $.id(R.id.bjy_base_setting_type_beauty).view().setSelected(true);
        settingContainer.removeAllViews();
        LayoutInflater.from(context).inflate(R.layout.bjy_base_layout_setting_beauty, settingContainer);
        initBeautyContainer();
    }

    AppCompatImageView beautyIcon;
    AppCompatImageView whitenessIcon;
    AppCompatSeekBar beautySeekbar;
    AppCompatSeekBar whitenessSeekbar;
    protected SettingSwitch beautySwitch;

    protected void initBeautyContainer(){
        $.clearRefCache();
        beautySwitch = $.id(R.id.bjy_base_setting_beauty_switch).view();

        beautyIcon = $.id(R.id.bjy_base_setting_beauty_icon).view();
        StateListDrawable beautyDrawable = new StateListDrawableBuilder()
                .normal(AppCompatResources.getDrawable(context, R.drawable.bjy_base_vector_beauty_off))
                .selected(AppCompatResources.getDrawable(context, R.drawable.bjy_base_vector_beauty_on))
                .build();
        beautyIcon.setImageDrawable(beautyDrawable);

        whitenessIcon = $.id(R.id.bjy_base_setting_whiteness_icon).view();
        StateListDrawable whitenessDrawable = new StateListDrawableBuilder()
                .normal(AppCompatResources.getDrawable(context, R.drawable.bjy_base_vector_whiteness_off))
                .selected(AppCompatResources.getDrawable(context, R.drawable.bjy_base_vector_whiteness_on))
                .build();
        whitenessIcon.setImageDrawable(whitenessDrawable);

        beautySeekbar = $.id(R.id.bjy_base_setting_beauty_seekbar).view();
        beautySeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                lastBeautyLevel = seekBar.getProgress() * 1.f / seekBar.getMax();
                presenter.setBeautyLevel(lastBeautyLevel);
            }
        });

        whitenessSeekbar = $.id(R.id.bjy_base_setting_whiteness_seekbar).view();
        whitenessSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                lastWhitenessLevel = seekBar.getProgress() * 1.f / seekBar.getMax();
                presenter.setWhitenessLevel(lastWhitenessLevel);
            }
        });

        if (presenter.isVideoAttached()) {
            if (isBeautySwitchCheck) {
                beautySwitch.setChecked(true);
                updateBeautyStatus(true);
                beautySeekbar.setProgress((int) (presenter.getBeautyLevel() * 100));
                whitenessSeekbar.setProgress((int) (presenter.getWhitenessLevel() * 100));
            } else {
                beautySwitch.setChecked(false);
                updateBeautyStatus(false);
            }
        } else {
            beautySwitch.setChecked(false);
            beautySwitch.setEnabled(false);
            updateBeautyStatus(false);
        }
        beautySwitch.setOnCheckedChangeListener((MultiSwitchCompat.OnCheckedChangeListener) (buttonView, checkedPosition) -> {
            if (checkedPosition == 0) {
                beautySeekbar.setProgress((int) (lastBeautyLevel * 100));
                presenter.setBeautyLevel(lastBeautyLevel);
                whitenessSeekbar.setProgress((int) (lastWhitenessLevel * 100));
                presenter.setWhitenessLevel(lastWhitenessLevel);
            } else {
                beautySeekbar.setProgress(0);
                whitenessSeekbar.setProgress(0);
                presenter.setBeautyLevel(0);
                presenter.setWhitenessLevel(0);
            }
            updateBeautyStatus(checkedPosition == 0);
            isBeautySwitchCheck = (checkedPosition == 0);
        });
    }

    private void updateBeautyStatus(boolean enable) {
        whitenessIcon.setSelected(enable);
        beautyIcon.setSelected(enable);
        beautySeekbar.setEnabled(enable);
        whitenessSeekbar.setEnabled(enable);
        if (enable) {
            beautySeekbar.setThumb(context.getResources().getDrawable(R.drawable.bjy_base_seekbar_thumb));
            whitenessSeekbar.setThumb(context.getResources().getDrawable(R.drawable.bjy_base_seekbar_thumb));
        } else {
            beautySeekbar.setThumb(new ColorDrawable(Color.TRANSPARENT));
            whitenessSeekbar.setThumb(new ColorDrawable(Color.TRANSPARENT));
        }
    }

    private void showOtherContainer() {
        ((TextView) $.id(R.id.bjy_base_setting_type_other).view()).setTextColor(Color.WHITE);
        lastSelectedView.setTextColor(ThemeDataUtil.getColorFromThemeConfigByAttrId(context, R.attr.base_theme_window_main_text_color));
        lastSelectedView.setSelected(false);
        $.id(R.id.bjy_base_setting_type_other).view().setSelected(true);
        settingContainer.removeAllViews();
        LayoutInflater.from(context).inflate(R.layout.bjy_base_layout_setting_other, settingContainer);
        initOtherContainer();
    }

    protected abstract void initOtherContainer();

    protected void hideRoomControlMenu() {
        $.id(R.id.bjy_base_setting_forbid_raise_hands_text).visibility(View.GONE);
        $.id(R.id.bjy_base_setting_forbid_raise_hands_switch).visibility(View.GONE);
        $.id(R.id.bjy_base_setting_forbid_all_text).visibility(View.GONE);
        $.id(R.id.bjy_base_setting_forbid_all_switch).visibility(View.GONE);
        $.id(R.id.bjy_base_setting_all_horizontal_mirror_text).visibility(View.GONE);
        $.id(R.id.bjy_base_setting_all_horizontal_mirror_open).visibility(View.GONE);
        $.id(R.id.bjy_base_setting_all_horizontal_mirror_close).visibility(View.GONE);
        $.id(R.id.bjy_base_setting_all_vertical_mirror_text).visibility(View.GONE);
        $.id(R.id.bjy_base_setting_all_vertical_mirror_open).visibility(View.GONE);
        $.id(R.id.bjy_base_setting_all_vertical_mirror_close).visibility(View.GONE);
    }

    private void updateBeautyVisible() {
        boolean isSupportBeauty = presenter.getWebrtcType() >= 3;
        if (isSupportBeauty) {
            $.id(R.id.bjy_base_setting_type_beauty).visible();
        } else {
            $.id(R.id.bjy_base_setting_type_beauty).gone();
        }
    }

    @Override
    public void setCameraEnable(boolean isEnable) {
        $.id(R.id.bjy_base_setting_camera_open_switch).enable(isEnable);
    }

    @Override
    public void showCamera(boolean videoAttached) {
        $.id(R.id.bjy_base_setting_camera_open_switch).check(videoAttached);
    }

    @Override
    public void showCameraSwitchStatus(boolean whetherShow) {
        setCameraAboutEnable(whetherShow);
    }

    @Override
    public void setCameraAboutEnable(boolean isEnable) {
        setWhichCameraEnable(isEnable && !presenter.isReplaceCamera());
    }

    @Override
    public void setWhichCameraEnable(boolean isEnable) {
        $.id(R.id.bjy_base_setting_camera_orientation_switch).enable(isEnable);
    }

    @Override
    public void showAudio(boolean isAudioBackOpen) {
        $.id(R.id.bjy_base_setting_background_audio_switch).check(isAudioBackOpen);
    }

    @Override
    public void showVisitorFail() {
        showToast(getString(R.string.bjy_base_media_visitor_fail));
    }

    @Override
    public void setRemarkSettingEnable(boolean isEnable) {
        $.id(R.id.bjy_base_setting_ppt_remark_switch).enable(isEnable);
    }

    @Override
    public void showForbidAllAudio(boolean forbidAllAudioStatus) {
        $.id(R.id.bjy_base_setting_forbid_all_audio_switch).check(forbidAllAudioStatus);
    }

    @Override
    public void showForbidRaiseHand(boolean forbidRaiseHandStatus) {
        $.id(R.id.bjy_base_setting_forbid_raise_hands_switch).check(forbidRaiseHandStatus);
    }

    @Override
    public void setForbiddenEnable(boolean isEnable) {
        $.id(R.id.bjy_base_setting_forbid_all_switch).enable(isEnable);
    }

    @Override
    public void showForbidden(boolean isForbid) {
        $.id(R.id.bjy_base_setting_forbid_all_switch).check(isForbid);
    }

    @Override
    public void showSwitchForbid() {
        showToast(getString(R.string.bjy_base_room_forbid_and_kick_permission_forbid));
    }

    @Override
    public void setForbidRaiseHandEnable(boolean isEnable) {
        $.id(R.id.bjy_base_setting_forbid_raise_hands_switch).enable(isEnable);
    }

    @Override
    public void setMicEnable(boolean isEnable) {
        $.id(R.id.bjy_base_setting_mic_open_switch).enable(isEnable);
    }

    @Override
    public void setForbidAllAudioEnable(boolean isEnable) {
        $.id(R.id.bjy_base_setting_forbid_all_audio_switch).enable(isEnable);
    }

    @Override
    public void setPPTShowTypeEnable(boolean isEnable) {
        $.id(R.id.bjy_base_setting_ppt_show_type_switch).enable(isEnable);
    }

    @Override
    public void setPPTViewTypeEnable(boolean isEnable) {
        $.id(R.id.bjy_base_setting_ppt_view_type_switch).enable(isEnable);
    }

    @Override
    public void setDefinitionEnable(boolean isEnable) {
        $.id(R.id.bjy_base_setting_camera_quality_switch).enable(isEnable);
    }

    @Override
    public void setUpLinkEnable(boolean isEnable) {
        $.id(R.id.bjy_base_setting_upload_load_switch).enable(isEnable);
    }

    @Override
    public void setDownLinkEnable(boolean isEnable) {
        $.id(R.id.bjy_base_setting_download_load_switch).enable(isEnable);
    }

    @Override
    public void setAudioEnable(boolean isEnable) {
        $.id(R.id.bjy_base_setting_background_audio_switch).enable(isEnable);
    }

    @Override
    public void setChangePPTEnable(boolean isEnable) {
        $.id(R.id.bjy_base_setting_student_preview_switch).enable(isEnable);
    }

    @Override
    public void showStudentsHandsUp(boolean isEnable) {
        setMicEnable(isEnable);
        setCameraEnable(isEnable);
    }

    @Override
    public void hidePPTMenu() {
        $.id(R.id.bjy_base_setting_ppt_view_type_text).visibility(View.GONE);
        $.id(R.id.bjy_base_setting_ppt_view_type_switch).visibility(View.GONE);
        $.id(R.id.bjy_base_setting_ppt_remark_text).visibility(View.GONE);
        $.id(R.id.bjy_base_setting_ppt_remark_switch).visibility(View.GONE);
    }

    @Override
    public void hidePPTChangeMenu() {
        $.id(R.id.bjy_base_setting_student_preview_text).visibility(View.GONE);
        $.id(R.id.bjy_base_setting_student_preview_switch).visibility(View.GONE);
    }

    @Override
    public void showUpLinkType(boolean isTCP) {
        $.id(R.id.bjy_base_setting_upload_load_switch).check(isTCP);
    }

    @Override
    public void showDownLinkType(boolean isTCP) {
        $.id(R.id.bjy_base_setting_download_load_switch).check(isTCP);
    }

    @Override
    public void showPPTViewType(boolean isAnim) {
        $.id(R.id.bjy_base_setting_ppt_view_type_switch).check(isAnim);
    }

    @Override
    public void showPPTViewNoPPT() {
        $.id(R.id.bjy_base_setting_ppt_view_type_switch).enable(false);
    }

    @Override
    public void showMic(boolean audioAttached) {
        $.id(R.id.bjy_base_setting_mic_open_switch).check(audioAttached);
    }

    @Override
    public void showChangePPTEnable(boolean studentSwitchPPTState) {
        $.id(R.id.bjy_base_setting_student_preview_switch).check(studentSwitchPPTState);
    }

    @Override
    public void showPPTType(boolean isFullScreen) {
        $.id(R.id.bjy_base_setting_ppt_show_type_switch).check(isFullScreen);
    }

    @Override
    public void showWhichCamera(boolean cameraOrientation) {
        $.id(R.id.bjy_base_setting_camera_orientation_switch).check(cameraOrientation);
    }

    @Override
    public void hidePPTShownType() {
        $.id(R.id.bjy_base_setting_ppt_view_type_switch).check(true);
        setPPTShowTypeEnable(false);
    }

    @Override
    public void onMirrorModeChange(LPMirrorModeModel mirrorModeModel, Set<String> horizontalSet, Set<String> verticalSet) {
        if (mirrorModeModel != null && mirrorModeModel.isToAll) {
            if (mirrorModeModel.horizonMirrorMode == 1) {
                $.id(R.id.bjy_base_setting_all_horizontal_mirror_open).enable(false);
                $.id(R.id.bjy_base_setting_all_horizontal_mirror_open).setSelected(true);
                $.id(R.id.bjy_base_setting_all_horizontal_mirror_close).enable(true);
                $.id(R.id.bjy_base_setting_all_horizontal_mirror_close).setSelected(false);
            } else {
                $.id(R.id.bjy_base_setting_all_horizontal_mirror_close).enable(false);
                $.id(R.id.bjy_base_setting_all_horizontal_mirror_close).setSelected(true);
                $.id(R.id.bjy_base_setting_all_horizontal_mirror_open).enable(true);
                $.id(R.id.bjy_base_setting_all_horizontal_mirror_open).setSelected(false);
            }
            if (mirrorModeModel.verticalMirrorMode == 1) {
                $.id(R.id.bjy_base_setting_all_vertical_mirror_open).enable(false);
                $.id(R.id.bjy_base_setting_all_vertical_mirror_open).setSelected(true);
                $.id(R.id.bjy_base_setting_all_vertical_mirror_close).enable(true);
                $.id(R.id.bjy_base_setting_all_vertical_mirror_close).setSelected(false);
            } else {
                $.id(R.id.bjy_base_setting_all_vertical_mirror_close).enable(false);
                $.id(R.id.bjy_base_setting_all_vertical_mirror_close).setSelected(true);
                $.id(R.id.bjy_base_setting_all_vertical_mirror_open).enable(true);
                $.id(R.id.bjy_base_setting_all_vertical_mirror_open).setSelected(false);
            }
        } else {
            if (mirrorModeModel == null) {
                $.id(R.id.bjy_base_setting_all_vertical_mirror_open).enable(true);
                $.id(R.id.bjy_base_setting_all_vertical_mirror_open).setSelected(false);
                $.id(R.id.bjy_base_setting_all_horizontal_mirror_open).enable(true);
                $.id(R.id.bjy_base_setting_all_horizontal_mirror_open).setSelected(false);
            } else {
                //改的垂直
                if (mirrorModeModel.horizonMirrorMode == lastMirrorModeModel.horizonMirrorMode) {
                    $.id(R.id.bjy_base_setting_all_vertical_mirror_open).enable(true);
                }
                //改的水平
                if (mirrorModeModel.verticalMirrorMode == lastMirrorModeModel.verticalMirrorMode) {
                    $.id(R.id.bjy_base_setting_all_horizontal_mirror_open).enable(true);
                }
            }
            $.id(R.id.bjy_base_setting_all_horizontal_mirror_close).enable(!horizontalSet.isEmpty());
            $.id(R.id.bjy_base_setting_all_vertical_mirror_close).enable(!verticalSet.isEmpty());
        }
        if (mirrorModeModel != null) {
            lastMirrorModeModel = mirrorModeModel;
        }
    }

    @Override
    public void onChangePPTForbidFailed() {
        showToast(getString(R.string.bjy_base_cant_change_ppt_fail_tip));
    }

    @Override
    public void showSwitchPPTFail() {
        showToast(getString(R.string.bjy_base_ppt_from_teacher_tip));
    }

    @Override
    public void showSwitchLinkTypeError() {
        showToast(getString(R.string.bjy_base_switch_link_type_error));
    }

    protected boolean checkClickable(String error) {
        if (!clickableWithKey.containsKey(error)) {
            clickableWithKey.put(error, new AtomicBoolean(true));
        }
        final AtomicBoolean clickable = clickableWithKey.get(error);
        if (clickable != null && clickable.get()) {
            clickable.set(false);
            disposableOfTimer = Observable.timer(2, TimeUnit.SECONDS).subscribe(aLong ->{
                clickable.set(true);
                LPRxUtils.dispose(disposableOfTimer);
            } );
            return true;
        } else {
            showToast(error);
            return false;
        }
    }

    protected List<String> getShowResolutionList(LPConstants.LPResolutionType maxResolutionType) {
        List<String> list;
        switch (maxResolutionType) {
            case _360:
                list = Arrays.asList(getString(R.string.bjy_base_setting_definition_low), getString(R.string.bjy_base_setting_definition_360));
                break;
            case HIGH:
                list = Arrays.asList(getString(R.string.bjy_base_setting_definition_low),
                        getString(R.string.bjy_base_setting_definition_360),
                        getString(R.string.bjy_base_setting_definition_high));
                break;
            case _720:
                list = Arrays.asList(getString(R.string.bjy_base_setting_definition_low),
                        getString(R.string.bjy_base_setting_definition_360),
                        getString(R.string.bjy_base_setting_definition_high),
                        getString(R.string.bjy_base_setting_definition_720));
                break;
            case _1080:
                list = Arrays.asList(getString(R.string.bjy_base_setting_definition_low),
                        getString(R.string.bjy_base_setting_definition_360),
                        getString(R.string.bjy_base_setting_definition_high),
                        getString(R.string.bjy_base_setting_definition_720),
                        getString(R.string.bjy_base_setting_definition_1080));
                break;
            case LOW:
            default:
                list = Arrays.asList(getString(R.string.bjy_base_setting_definition_low));
                break;
        }
        return list;
    }

    protected List<LPConstants.LPResolutionType> getResolutionList(LPConstants.LPResolutionType maxResolutionType) {
        List<LPConstants.LPResolutionType> list;
        switch (maxResolutionType) {
            case _360:
                list = Arrays.asList(LPConstants.LPResolutionType.LOW, LPConstants.LPResolutionType._360);
                break;
            case HIGH:
                list = Arrays.asList(LPConstants.LPResolutionType.LOW,
                        LPConstants.LPResolutionType._360,
                        LPConstants.LPResolutionType.HIGH);
                break;
            case _720:
                list = Arrays.asList(LPConstants.LPResolutionType.LOW,
                        LPConstants.LPResolutionType._360,
                        LPConstants.LPResolutionType.HIGH,
                        LPConstants.LPResolutionType._720);
                break;
            case _1080:
                list = Arrays.asList(LPConstants.LPResolutionType.LOW,
                        LPConstants.LPResolutionType._360,
                        LPConstants.LPResolutionType.HIGH,
                        LPConstants.LPResolutionType._720,
                        LPConstants.LPResolutionType._1080);
                break;
            case LOW:
            default:
                list = Arrays.asList(LPConstants.LPResolutionType.LOW);
                break;
        }
        return list;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LPRxUtils.dispose(disposableOfTimer);
    }

    protected abstract BaseSettingPresenter initPresenter();

    protected abstract void initChildData();
}
